<template>
  <div>
    <table width="500" border="1">
      <thead>
        <tr>
          <th>ID</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="obj in list" :key="obj.id">
          <td>{{obj.id}}</td>
          <td>{{obj.bookname}}</td>
          <td>{{obj.author}}</td>
          <td>{{obj.publisher}}</td>
        </tr>
      </tbody>
    </table>
    <form action="" @submit.prevent="onsubmit">
      <input type="text" v-model="bookname" placeholder="bookname">
      <input type="text" v-model="author" placeholder="author">
      <input type="text" v-model="publisher" placeholder="publisher">
      <button>添加</button>
    </form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      list: [],
      bookname: '',
      author: '',
      publisher: '',
      appkey: '7250d3eb-18e1-41bc-8bb2-11483665535a'
    }
  },
  async created () {
    const { data } = await this.$axios('/api/getbooks')
    this.list = data.data
  },
  methods: {
    async onsubmit () {
      if (this.bookname === '' || this.author === '' || this.publisher === '') return alert('请填写选项')
      const { data } = await this.$axios({
        url: '/api/addbook',
        method: 'POST',
        data: {
          bookname: this.bookname,
          author: this.author,
          publisher: this.publisher,
          appkey: this.appkey
        }
      })
      if (data.status !== 201) return alert(data.msg)
      this.bookname = ''
      this.author = ''
      this.publisher = ''
    }
  }
}
</script>

<style>

</style>
